<template>
  <!-- 排序 -->
  <div class="sub-sort">
    <div class="sort">
      <a
        :class="{ active: data.sortField === null }"
        @click="changeSort(null)"
        href="javascript:;"
        >默认排序</a
      >
      <a
        :class="{ active: data.sortField === 'publishTime' }"
        @click="changeSort('publishTime')"
        href="javascript:;"
        >最新商品</a
      >
      <a
        :class="{ active: data.sortField === 'orderNum' }"
        @click="changeSort('orderNum')"
        href="javascript:;"
        >最高人气</a
      >
      <a
        :class="{ active: data.sortField === 'evaluateNum' }"
        @click="changeSort('evaluateNum')"
        href="javascript:;"
        >评论最多</a
      >
      <a @click="changeSort('price')" href="javascript:;">
        价格排序
        <i class="arrow up"  :class="{ active: data.sortField === 'price'&&data.sortMethod == 'asc' }"/>
        <i class="arrow down"  :class="{ active: data.sortField === 'price'&&data.sortMethod == 'desc'}" />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox @change="checkChange" v-model="data.inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox @change="checkChange" v-model="data.onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>
<script>
import { reactive } from "vue";
export default {
  name: "SubSort",
  setup(props,{emit}) {
    // 定义数据与后台相对应
    const data = reactive({
      inventory: false, //是否有库存
      onlyDiscount: false, //只显示特惠
      sortField: null, //排序字段
      sortMethod: null, //排序规则desc,asc
    });

    // 点击事件
    const changeSort = (sortField) => {
      // 判断是不是价格
      if (data.sortField === "price") {
        data.sortField = sortField
        // 默认下降
        if(data.sortMethod === null){
          data.sortMethod = 'desc'
        }else{
          data.sortMethod = data.sortMethod === 'desc' ? 'asc' : 'desc'
        }

      } else {
        // 不是价格
        // 如果排序未改变停止逻辑 
        // 少几句注释的代码 
        // if (data.sortField === sortField)  return
        data.sortField = sortField
        data.sortMethod = null//没有价格时  为空
        // 触发自定义事件 知识点：子组件向父组件传递数据
        emit('sort-change',data)
      }
    };

    const checkChange = (sortField) => {
        emit('sort-change',data)
      }
    return { data, changeSort,checkChange };
  },
};
</script>
<style  lang='less' scoped>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>